package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.dto.LaboratoryCheckDto;
import com.zito.health.dto.LaboratoryCheckPrintDto;
import com.zito.health.entity.LaboratoryCheck;
import com.zito.health.entity.User;
import com.zito.health.service.LaboratoryCheckService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.LaboratoryCheckVo;
import com.zito.health.vo.LaboratoryInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @description: 实验室校验
 * @author: Sam
 * @time: 2020/02/28 23:00
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/laboratoryCheck")
public class LaboratoryCheckController {

    @Autowired
    private LaboratoryCheckService laboratoryCheckService;

    /**
     * 实验室校验添加
     * @param laboratoryCheck
     * @return
     */
    @PostMapping("/saveBase")
    public VerifyResult saveBase(LaboratoryCheck laboratoryCheck){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryCheck.setDeclareTime(new Date());
        laboratoryCheckService.save(laboratoryCheck);
        return verifyResult;
    }

    /**
     *
     * @param laboratoryCheck
     * @return
     */
    @PutMapping
    public VerifyResult update(LaboratoryCheck laboratoryCheck){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryCheckService.update(laboratoryCheck);
        return verifyResult;
    }

    /**
     * 列表
     * @param laboratoryCheckDto
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult list(LaboratoryCheckDto laboratoryCheckDto, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        User user = CacheUtil.getCacheUser().getUser();
        if(user.getRoleId()==1){//1.admin
        }else if(user.getRoleId()==2) {//2市局
            laboratoryCheckDto.setStatesStr("2");
        }else if(user.getRoleId()==3){//县局
            laboratoryCheckDto.setUnitId(user.getUnitId());
            laboratoryCheckDto.setStatesStr("1,3,5");
        }else if(user.getRoleId()==9){//实验室单位管理员
            laboratoryCheckDto.setUnitId(user.getUnitId());
            laboratoryCheckDto.setStatesStr("1,2,3,5,6");//审核状态：1.未审核，2.县/区审核已通过，3.县/区审核未通过，4.市审核已通，5.市审核未通过，6.未提交
        }
        PageHelper.startPage(pageNo,pageSize);
        List<LaboratoryCheckDto> list = laboratoryCheckService.queryLaboratoryCheck(laboratoryCheckDto);
        PageInfo<LaboratoryCheckDto> pageInfo = new PageInfo(list);
        PageResult<LaboratoryCheckDto> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    /**
     * 列表
     * @param laboratoryCheckDto
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/4LabList")
    public PageResult forLabList(LaboratoryCheckDto laboratoryCheckDto, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        PageHelper.startPage(pageNo,pageSize);
        List<LaboratoryCheckDto> list = laboratoryCheckService.queryLaboratoryCheck(laboratoryCheckDto);
        PageInfo<LaboratoryCheckDto> pageInfo = new PageInfo(list);
        PageResult<LaboratoryCheckDto> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @GetMapping("/getLabInfoByLabId/{laboratoryId}")
    public LaboratoryInfoVo getLabInfoByLabId(@PathVariable Integer laboratoryId){
        LaboratoryInfoVo laboratoryInfoVo = laboratoryCheckService.getLabInfoByLabId(laboratoryId);
        laboratoryInfoVo.setCheckTime(new Date());
        return laboratoryInfoVo;
    }

    @GetMapping("/getLabInfoByLabCheckId/{checkId}")
    public LaboratoryCheckVo getLabInfoByLabCheckId(@PathVariable Integer checkId){
        LaboratoryCheckVo laboratoryCheckVo = laboratoryCheckService.getLabInfoByLabCheckId(checkId);
        return laboratoryCheckVo;
    }

    /**
     * 实验室变更删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}/{laboratoryId}")
    public VerifyResult delete(@PathVariable Integer id,@PathVariable Integer laboratoryId){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryCheckService.deleteById(id,laboratoryId);
        return verifyResult;
    }
    /**
     * 实验室变更删除
     * @param checkId
     * @return
     */
    @GetMapping("/print/{checkId}")
    public LaboratoryCheckPrintDto print(@PathVariable Integer checkId){
        return laboratoryCheckService.print(checkId);
    }

}
